import datetime
import database


# all overdue books and fines
def book_overdue():
    book_info = database.book_overdue()
    overdue_book_info = []
    for item in book_info:
        if item[4] is None or item[4] == "":
            # Date formatting
            checkout_date = datetime.datetime.strptime(item[3], "%Y-%m-%d")
            # get the now time
            now = datetime.datetime.now()
            # Calculate the borrowing time
            borrow_days = now - checkout_date
            days = borrow_days.days
            # Calculate fines
            if days > item[1]:
                fines = (days - item[1]) * 0.25
                overdue_book_info.append([item[0], item[2], fines])
            else:
                book_info.remove(item)
        else:
            # Date formatting
            checkout_date = datetime.datetime.strptime(item[3], "%Y-%m-%d")
            return_date = datetime.datetime.strptime(item[4], "%Y-%m-%d")
            # Calculate the borrowing time
            borrow_days = return_date - checkout_date
            days = borrow_days.days
            # Calculate fines
            if days > item[1]:
                fines = (days - item[1]) * 0.25
                overdue_book_info.append([item[0], item[2], fines])
            else:
                book_info.remove(item)
    return overdue_book_info
    # print(overdue_book_info)


if __name__ == "__main__":
    # run this will show all overdue books
    book_overdue()
